<template>
  <div class="mod-plug__pluginvite">
    <el-form :inline="true" :model="state.dataForm" @keyup.enter="state.getDataList()">
      <el-form-item>
        <el-input v-model="state.dataForm.inviteCode" placeholder="邀请码" clearable></el-input>
      </el-form-item>
      <el-form-item>
        <el-input v-model="state.dataForm.title" placeholder="标题" clearable></el-input>
      </el-form-item>
      <el-form-item>
        <el-input v-model="state.dataForm.description" placeholder="描述" clearable></el-input>
      </el-form-item>
      <el-form-item>
        <el-input v-model="state.dataForm.qrCode" placeholder="二维码" clearable></el-input>
      </el-form-item>
      <el-form-item>
        <el-input v-model="state.dataForm.link" placeholder="跳转链接" clearable></el-input>
      </el-form-item>
      <el-form-item>
        <el-input v-model="state.dataForm.inviteUrl" placeholder="邀请链接" clearable></el-input>
      </el-form-item>
      <el-form-item>
        <el-input v-model="state.dataForm.times" placeholder="点击次数" clearable></el-input>
      </el-form-item>
      <el-row>
        <el-col>
          <el-form-item>
            <el-button @click="state.getDataList()">{{ $t("query") }}</el-button>
          </el-form-item>
          <el-form-item>
            <el-button type="info" @click="state.exportHandle()">{{ $t("export") }}</el-button>
          </el-form-item>
          <el-form-item>
            <el-button v-if="state.hasPermission('plug:pluginvite:save')" type="primary" @click="addOrUpdateHandle()">{{ $t("add") }}</el-button>
          </el-form-item>
          <el-form-item>
            <el-button v-if="state.hasPermission('plug:pluginvite:delete')" type="danger" @click="state.deleteHandle()">{{ $t("deleteBatch") }}</el-button>
          </el-form-item>
        </el-col>
      </el-row>
    </el-form>
    <el-table v-loading="state.dataListLoading" :data="state.dataList" border @selection-change="state.dataListSelectionChangeHandle" style="width: 100%">
      <el-table-column type="selection" header-align="center" align="center" width="50"></el-table-column>
      <!-- <el-table-column prop="id" label="ID" header-align="center" align="center"></el-table-column> -->
      <!-- <el-table-column prop="inviteCode" label="邀请码" header-align="center" align="center"></el-table-column> -->
      <el-table-column prop="title" label="标题" header-align="center" align="center"></el-table-column>
      <el-table-column prop="description" label="描述" header-align="center" align="center"></el-table-column>
      <el-table-column prop="qrCode" label="二维码" header-align="center" align="center"></el-table-column>
      <el-table-column prop="link" label="跳转链接" header-align="center" align="center"></el-table-column>
      <el-table-column prop="inviteUrl" label="邀请链接" header-align="center" align="center"></el-table-column>
      <el-table-column prop="times" label="点击次数" header-align="center" align="center"></el-table-column>
      <el-table-column prop="remark" label="描述" header-align="center" align="center"></el-table-column>
      <el-table-column prop="status" label="状态" header-align="center" align="center">
        <template v-slot="scope">
          <el-tag v-if="scope.row.status" size="small" type="success">生效</el-tag>
          <el-tag v-else type="danger">失效</el-tag>
        </template>
      </el-table-column>
      <el-table-column prop="num" label="排序" header-align="center" align="center"></el-table-column>
      <el-table-column :label="$t('handle')" fixed="right" header-align="center" align="center" width="150">
        <template v-slot="scope">
          <el-button v-if="state.hasPermission('plug:pluginvite:update')" type="primary" link @click="addOrUpdateHandle(scope.row.id)">{{ $t("update") }}</el-button>
          <el-button v-if="state.hasPermission('plug:pluginvite:delete')" type="primary" link @click="state.deleteHandle(scope.row.id)">{{ $t("delete") }}</el-button>
        </template>
      </el-table-column>
    </el-table>
    <el-pagination :current-page="state.page" :page-sizes="[10, 20, 50, 100]" :page-size="state.limit" :total="state.total" layout="total, sizes, prev, pager, next, jumper" @size-change="state.pageSizeChangeHandle" @current-change="state.pageCurrentChangeHandle"> </el-pagination>
    <!-- 弹窗, 新增 / 修改 -->
    <add-or-update :key="addKey" ref="addOrUpdateRef" @refreshDataList="state.getDataList"></add-or-update>
  </div>
</template>

<script lang="ts" setup>
import useView from "@/hooks/useView";
import { nextTick, reactive, ref, toRefs, watch } from "vue";
import AddOrUpdate from "./pluginvite-add-or-update.vue";

const view = reactive({
  getDataListURL: "/plug/pluginvite/page",
  getDataListIsPage: true,
  exportURL: "/plug/pluginvite/export",
  deleteURL: "/plug/pluginvite",
  deleteIsBatch: true,
  dataForm: {
    inviteCode: "",
    title: "",
    description: "",
    qrCode: "",
    link: "",
    inviteUrl: "",
    times: "",
  }
});

const state = reactive({ ...useView(view), ...toRefs(view) });


const addKey = ref(0);
const addOrUpdateRef = ref();
const addOrUpdateHandle = (id?: number) => {
  addKey.value++;
  nextTick(() => {
    addOrUpdateRef.value.init(id);
  });
};
</script>